from django.db import models
from utils.modelsMixin import ModelSetMixin
from django.contrib.auth.models import User  # auth自带的用户表
from work.models import Label  # 标签

# Create your models here.

# 文章类
class Article(ModelSetMixin):
    STATUS_CHOICES = (
        (0,'未发布'),
        (1,'发布'),
    )
    title = models.CharField(max_length=100, verbose_name='标题')
    digest = models.CharField(max_length=300, verbose_name='摘要')
    content = models.TextField(verbose_name='文章内容')
    page_view = models.IntegerField(verbose_name='浏览量',default=0)
    priority = models.IntegerField(verbose_name='优先级',default=0)
    status = models.IntegerField(verbose_name='状态',default=0, choices=STATUS_CHOICES)

    # 一对多，标签对应多篇文章
    label = models.ForeignKey(Label,on_delete=models.CASCADE)
    # 一对多，用户对应多篇文章
    user = models.ForeignKey(User,on_delete=models.CASCADE)

    class Meta:
        # 排序
        ordering = ['-priority','-page_view','-create_time']
        db_table = 'article'  # 设置表名
        verbose_name = '文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.id},{self.title}'

# 评论模型类
class Comment(ModelSetMixin):
    content = models.TextField(verbose_name='评论内容')
    level = models.IntegerField( verbose_name='评论等级',default=1)
    parent_comment = models.IntegerField( verbose_name='父级评论',null=True)
    reply_comment = models.IntegerField( verbose_name='回复评论',null=True)
    # 一对多联系
    article = models.ForeignKey(Article,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)

    class Meta:
        # 排序
        ordering = ['-create_time']
        db_table = 'comment'  # 设置表名
        verbose_name = '评论'
        verbose_name_plural = verbose_name